package autoorder.zsxmlv.util;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 * @Author: ZSXM
 * @Description: java制作图片
 * @Company: QIDI
 * @Date: Create by 2018/9/2 12:34
 * @Version: 1.0
 * @Modify by:
 */
public class MakePicUtils {

    private BufferedImage image;
    private int imageWidth = 250;  //图片的宽度
    private int imageHeight = 450; //图片的高度


    public void createImage(String fileLocation) {
        BufferedOutputStream bos = null;
        if(image != null){
            try {
                FileOutputStream fos = new FileOutputStream(fileLocation);
                bos = new BufferedOutputStream(fos);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                encoder.encode(image);
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(bos!=null){//关闭输出流
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void graphicsGeneration(String name, String id, String classname, String imgurl) {

        int logoW = 240;
        int logoH = 80;

        // 创建图片并填充白色背景
        image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D main = image.createGraphics();
        main.setBackground(Color.white);
        main.fillRect(0, 0, imageWidth, imageHeight);

        // logo
        Graphics2D imageGraphics = image.createGraphics();
        BufferedImage read = null;
        try {
            read = ImageIO.read(new URL("http://buy.ccb.com/public/ccb_icon/common/ccb_head_logo.png"));
            if (read != null) {
                imageGraphics.drawImage(read, 10, 0, logoW, logoH - 10, null);
                imageGraphics.dispose();
                read = null;
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        // 商品标题
        Graphics2D header = image.createGraphics();
        String str = "这是一堆堆的文字测试下会不会发生文字溢出如果溢出了就需要在索引处换行";
//        String str = "这是表土";
        header.setColor(Color.red);
        header.drawRect(10, logoH - 10, 230, str.length() > 18 ? 45 : 25);
        Font font = new Font("微软雅黑", Font.PLAIN, 12);
        header.setFont(font);
        header.setColor(Color.black);
        if (str.length() > 18) {
            String str2 = str.substring(0, 18);
            str = str.substring(18);
            header.drawString(str2, 15,logoH+5);
            header.drawString(str, 15,logoH+25);
        } else {
            header.drawString(str, 15, logoH+5);
        }
        Font fonttime = new Font("微软雅黑", Font.PLAIN, 10);
        header.setFont(fonttime);
        header.drawString("2018-09-08 14:06:55", 130, 140);

        // 商品价格 & 下单时间 & 下单账号
        header.setColor(Color.red);
        Font fontPrice = new Font("微软雅黑", Font.BOLD, 14);
        header.setFont(fontPrice);
        header.drawString("￥18.88", 15, 140);

        Graphics2D goodsPIC = image.createGraphics();
        try {
            String url = "https://ibsbjstar.ccb.com.cn/CCBIS/QR?QRCODE=9980011321680921472421";
//            url = java.net.URLEncoder.encode(url);
            read = ImageIO.read(new URL("http://image3.buy.ccb.com/merchant/201705/1005518169/1496915639148_3.png"));
            BufferedImage qrread = ImageIO.read(new URL("https://qr.api.cli.im/qr?data=https%253A%252F%252Fibsbjstar.ccb.com.cn%252FCCBIS%252FQR%253FQRCODE%253D9980011321680921472421&level=H&transparent=false&bgcolor=%23ffffff&forecolor=%23000000&blockpixel=12&marginblock=1&logourl=&size=280&kid=cliim&key=e6278179bd09b5d29debd157498d1f75"));
            BufferedImage read1 = ImageIO.read(new File("D:/qrimg.png"));
            if (read != null) {
                goodsPIC.drawImage(read, 25, 150, 200, 200, null);
                goodsPIC.drawImage(read1, 5, 355, 240, 85, null);
                goodsPIC.drawImage(qrread, 40, 367, 58, 58, null);
                goodsPIC.dispose();
                read = null;
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }




        createImage("d:\\hehe.jpg");

    }

    public static void main(String[] args) {
        MakePicUtils mk = new MakePicUtils();
        mk.graphicsGeneration("name","id","classname","E:/图片/1.jpg");
    }

}
